// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: qjs_mouse_event.cpp
// Description: Bindings for MouseEvent
//      Author: Ziming Li
//     Created: 2022-05-12
// -------------------------------------------------
// Copyright (C) 2022 MingYang Software Technology.
// -------------------------------------------------

#include "./qjs_bindings.h"

#include "blinkit/blink/renderer/bindings/core/qjs/qjs.h"
#include "blinkit/blink/renderer/bindings/core/qjs/qjs_c_function_list_entries.h"
#include "blinkit/blink/renderer/core/events/MouseEvent.h"

/**
 * https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
 */

namespace BlinKit {
namespace qjs {

static JSValue GetAltKey(JSContext *ctx, JSValueConst thisVal)
{
    MouseEvent *event = ScriptWrappable::Cast<MouseEvent>(thisVal);
    return JS_NewBool(ctx, event->altKey());
}

static JSValue GetButton(JSContext* ctx, JSValueConst thisVal)
{
    MouseEvent* event = ScriptWrappable::Cast<MouseEvent>(thisVal);
    return JS_NewInt32(ctx, event->button());
}

static JSValue GetClientX(JSContext *ctx, JSValueConst thisVal)
{
    MouseEvent *event = ScriptWrappable::Cast<MouseEvent>(thisVal);
    return JS_NewInt32(ctx, event->clientX());
}

static JSValue GetClientY(JSContext *ctx, JSValueConst thisVal)
{
    MouseEvent *event = ScriptWrappable::Cast<MouseEvent>(thisVal);
    return JS_NewInt32(ctx, event->clientY());
}

static JSValue GetCtrlKey(JSContext *ctx, JSValueConst thisVal)
{
    MouseEvent *event = ScriptWrappable::Cast<MouseEvent>(thisVal);
    return JS_NewBool(ctx, event->ctrlKey());
}

static JSValue GetShiftKey(JSContext *ctx, JSValueConst thisVal)
{
    MouseEvent *event = ScriptWrappable::Cast<MouseEvent>(thisVal);
    return JS_NewBool(ctx, event->shiftKey());
}

JSValue CreateMouseEventPrototype(JSContext *ctx, JSValue uiEventPrototype)
{
    static const JSCFunctionListEntry Funcs[] = {
        CGetSetEntry("altKey", GetAltKey, nullptr),
        CGetSetEntry("button", GetButton, nullptr),
        CGetSetEntry("clientX", GetClientX, nullptr),
        CGetSetEntry("clientY", GetClientY, nullptr),
        CGetSetEntry("ctrlKey", GetCtrlKey, nullptr),
        CGetSetEntry("shiftKey", GetShiftKey, nullptr)
    };

    JSValue ret = JS_NewObjectProto(ctx, uiEventPrototype);
    JS_SetPropertyFunctionList(ctx, ret, Funcs, std::size(Funcs));
    return ret;
}

} // namespace qjs
} // namespace BlinKit
